package com.abaaba.project2.service.wx.wxserviceimpl;

import com.abaaba.project2.bean.Category;
import com.abaaba.project2.bean.CategoryExample;
import com.abaaba.project2.bean.vo.wx.WXCatalogIndexLevelTwoShowListVO;
import com.abaaba.project2.bean.vo.wx.WXCatalogIndexShowListVO;
import com.abaaba.project2.bean.vo.wx.WXGoodsCategoryShowList;
import com.abaaba.project2.mapper.CategoryMapper;
import com.abaaba.project2.service.wx.WXCatalogIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WXCatalogIndexServiceImpl implements WXCatalogIndexService {

    @Autowired
    CategoryMapper categoryMapper;


    @Override
    public WXCatalogIndexShowListVO wxCatalogIndexShowList() {

        CategoryExample categoryExample = new CategoryExample();
        CategoryExample categoryExample1 = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        CategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
        criteria.andLevelEqualTo("L1");

        /*当前类属于你点什么类就传什么类*/

        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        ArrayList<WXCatalogIndexShowListVO.DataBean.CategoryListBean> categoryListBeans = new ArrayList<>();
        for (Category category : categoryList) {
            WXCatalogIndexShowListVO.DataBean.CategoryListBean categoryListBean = new WXCatalogIndexShowListVO.DataBean.CategoryListBean(category.getId(),category.getName(),category.getKeywords(),category.getDesc(),category.getPid(),category.getIconUrl(),category.getPicUrl(),category.getLevel(),category.getSortOrder(),category.getAddTime(),category.getUpdateTime(),category.getDeleted());
            categoryListBeans.add(categoryListBean);
        }
        //categoryListBeans
        //当前类是什么，先暂定，当前类是第一个对象
        WXCatalogIndexShowListVO.DataBean.CurrentCategoryBean currentCategoryBean = new WXCatalogIndexShowListVO.DataBean.CurrentCategoryBean(categoryList.get(0).getId(),categoryList.get(0).getName(),categoryList.get(0).getKeywords(),categoryList.get(0).getDesc(),categoryList.get(0).getPid(),categoryList.get(0).getIconUrl(),categoryList.get(0).getPicUrl(),categoryList.get(0).getLevel(),categoryList.get(0).getSortOrder(),categoryList.get(0).getAddTime(),categoryList.get(0).getUpdateTime(),categoryList.get(0).getDeleted());

        /*然后根据categoryList.get(0).getId() 再去取数据*/
        criteria1.andPidEqualTo(categoryList.get(0).getId());
        ArrayList<WXCatalogIndexShowListVO.DataBean.CurrentSubCategoryBean> currentSubCategoryBeans = new ArrayList<>();
        List<Category> categoryList1 = categoryMapper.selectByExample(categoryExample1);
        for (Category category : categoryList1) {
            WXCatalogIndexShowListVO.DataBean.CurrentSubCategoryBean currentSubCategoryBean = new WXCatalogIndexShowListVO.DataBean.CurrentSubCategoryBean(category.getId(),category.getName(),category.getKeywords(), category.getDesc(),category.getPid(),category.getIconUrl(),category.getPicUrl(),category.getLevel(),category.getSortOrder(),category.getAddTime(),category.getUpdateTime(),category.getDeleted());
            currentSubCategoryBeans.add(currentSubCategoryBean);
        }
        //currentSubCategoryBeans
        WXCatalogIndexShowListVO.DataBean dataBean = new WXCatalogIndexShowListVO.DataBean(currentCategoryBean,categoryListBeans,currentSubCategoryBeans);

        WXCatalogIndexShowListVO wxCatalogIndexShowListVO = new WXCatalogIndexShowListVO(0,dataBean,"成功");

        return wxCatalogIndexShowListVO;
    }

    @Override
    public WXCatalogIndexLevelTwoShowListVO wxCatalogIndexLevelTwoShowList(Integer id) {

        ArrayList<WXCatalogIndexLevelTwoShowListVO.DataBean.CurrentSubCategoryBean> currentSubCategoryBeans = new ArrayList<>();
        WXCatalogIndexLevelTwoShowListVO.DataBean.CurrentCategoryBean currentCategoryBean =null;
        List<Category> categoryList = categoryMapper.selectByIdOrPidOfCategory(id);
        for (Category category : categoryList) {
            if ("L1".equals(category.getLevel())){
                currentCategoryBean = new WXCatalogIndexLevelTwoShowListVO.DataBean.CurrentCategoryBean(category.getId(),category.getName(),category.getKeywords(),category.getDesc(),category.getPid(),category.getIconUrl(),category.getPicUrl(),category.getLevel(),category.getSortOrder(),category.getAddTime(),category.getUpdateTime(),category.getDeleted());
            }
            /*服装不能加进去*/
            WXCatalogIndexLevelTwoShowListVO.DataBean.CurrentSubCategoryBean currentSubCategoryBean = new WXCatalogIndexLevelTwoShowListVO.DataBean.CurrentSubCategoryBean(category.getId(),category.getName(),category.getKeywords(),category.getDesc(),category.getPid(),category.getIconUrl(),category.getPicUrl(),category.getLevel(),category.getSortOrder(),category.getAddTime(),category.getUpdateTime(),category.getDeleted());
            if (!"L1".equals(category.getLevel())){
            currentSubCategoryBeans.add(currentSubCategoryBean);
        }
        }

        WXCatalogIndexLevelTwoShowListVO.DataBean dataBean = new WXCatalogIndexLevelTwoShowListVO.DataBean(currentCategoryBean,currentSubCategoryBeans);

        WXCatalogIndexLevelTwoShowListVO wxCatalogIndexLevelTwoShowListVO = new WXCatalogIndexLevelTwoShowListVO(0,dataBean,"成功");
        WXGoodsShowListServiceImpl wxGoodsShowListService = new WXGoodsShowListServiceImpl();
        //wxGoodsShowListService.wxGoodsCategoryShow(id);
        return wxCatalogIndexLevelTwoShowListVO;
    }
}
